package com.liuceng.peatio.service

import com.liuceng.BaseSpec
import com.liuceng.peatio.mapper.MemberAccountFlowMapper
import com.liuceng.peatio.mapper.NewRptFrontAdGroupDailyMapper
import com.liuceng.peatio.mapper.NewRptFrontCampaignDailyMapper
import com.liuceng.peatio.model.FrontAdGroup
import com.liuceng.peatio.model.FrontCampaign
import com.liuceng.peatio.model.Member
import com.liuceng.peatio.model.NewRptFrontAdGroupDaily
import com.liuceng.peatio.model.NewRptFrontCampaignDaily
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.annotation.Rollback
import org.springframework.transaction.annotation.Transactional

import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter

/**
 * Created by yanwen on 16-5-30.
 */

@Transactional
class NewRptFrontAdGroupDailyServiceSpec extends BaseSpec{

    @Autowired
    MemberService memberService;
    @Autowired
    FrontAdGroupService frontAdGroupService;
    @Autowired
    MemberAccountFlowMapper memberAccountFlowMapper;
    @Autowired
    NewRptFrontAdGroupDailyMapper newRptFrontAdGroupDailyMapper;
    @Autowired
    NewRptFrontCampaignDailyMapper newRptFrontCampaignDailyMapper;
    @Autowired
    NewRptFrontAdGroupDailyService  newRptFrontAdGroupDailyService;
    @Autowired
    FrontCampaignService frontCampaignService;

    /*
    * 数据回滚
    * 再测试通过后数据回滚
    * */

    @Rollback
    def "测试单一账号根据日期初始化前台数据" (){
        given:
        NewRptFrontAdGroupDaily newRptAdGroup = new NewRptFrontAdGroupDaily(date: '2016-05-29',memberId: 57,cost: 1000,viewNum: 500,clickNum: 500)
        expect:
        newRptFrontAdGroupDailyService.initNewRpt(newRptAdGroup)
    }

    @Rollback
    def "测试 广告报表 计划报表 insertSelectivce()方法" () {
        given:
        NewRptFrontAdGroupDaily newRptAdGroup = new NewRptFrontAdGroupDaily(date: '2016-05-29',memberId: 57,cost: 1000,viewNum: 500,clickNum: 500)

        Map<String, Object> map = new HashMap<>();
        // 时间
        String rptdate = newRptAdGroup.getDate();
        LocalDate date = LocalDate.parse(rptdate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        map.put("rptYear", date.getYear());
        map.put("rptMonth", date.getMonthValue());
        map.put("rptDay", date.getDayOfMonth());
        map.put("memberId", newRptAdGroup.getMemberId());
        // 获取当前广告主
        Member member = memberService.find(newRptAdGroup.getMemberId());
        // 获取该客户下的第一条广告
        FrontAdGroup frontAdGroup = frontAdGroupService.findOneByParams(map);


        newRptAdGroup.setFrontAdGroupId(frontAdGroup.getId());
        newRptAdGroup.setFrontCampaignId(frontAdGroup.getFrontCampaignId());
        newRptAdGroup.setFrontAdGroupName(frontAdGroup.getTitle());

            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            newRptAdGroup.setRptDate(simple.parse(rptdate));
            newRptAdGroup.setRptDay(date.getDayOfMonth());
            newRptAdGroup.setRptMonth(date.getMonthValue());
            newRptAdGroup.setRptYear(date.getYear());
            newRptAdGroup.setCreatedAt(simpleDate.parse(rptdate + " 23:59:59"));
            newRptAdGroup.setUpdatedAt(simpleDate.parse(rptdate + " 23:59:59"));


        NewRptFrontCampaignDaily newRptFrontCampaignsDaily = new NewRptFrontCampaignDaily();


        map.put("id", frontAdGroup.getFrontCampaignId());
        FrontCampaign frontCampaign = frontCampaignService.findOneByParams(map);


        newRptFrontCampaignsDaily.setFrontCampaignId(frontCampaign.getId());
        newRptFrontCampaignsDaily.setFrontCampaignName(frontCampaign.getName());
        newRptFrontCampaignsDaily.setFrontCampaignType(frontCampaign.getCampaignType());
        newRptFrontCampaignsDaily.setMemberId(frontCampaign.getMemberId());

        newRptFrontCampaignsDaily.setClickNum(newRptAdGroup.getClickNum());
        newRptFrontCampaignsDaily.setCost(newRptAdGroup.getCost());
        newRptFrontCampaignsDaily.setViewNum(newRptAdGroup.getViewNum());
        newRptFrontCampaignsDaily.setRptDate(simple.parse(rptdate));
        newRptFrontCampaignsDaily.setRptDay(date.getDayOfMonth());
        newRptFrontCampaignsDaily.setRptMonth(date.getMonthValue());
        newRptFrontCampaignsDaily.setRptYear(date.getYear());

        newRptFrontCampaignsDaily.setCreatedAt(simpleDate.parse(rptdate + " 23:59:59"));
        newRptFrontCampaignsDaily.setUpdatedAt(simpleDate.parse(rptdate + " 23:59:59"));


        when:
        int total= newRptFrontAdGroupDailyMapper.insertSelective(newRptAdGroup);
        then:
        newRptAdGroup.id!=null
        total==1

        when:
        int total2=  newRptFrontCampaignDailyMapper.insertSelective(newRptFrontCampaignsDaily);
        then:
        newRptFrontCampaignsDaily.id!=null
        total2==1

    }


}
